package com.mobilesales.android.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;
import com.mobilesales.android.model.products.Category;
import com.mobilesales.android.model.products.Product;
import com.mobilesales.android.model.products.Tax;
import com.mobilesales.android.model.products.Unit;

public class ProductServiceClient extends ServiceClientBase {

        
        public ProductServiceClient(Configuration configuration) {
                super(configuration);
        }
        
        public Product getProduct(int productId) throws ServiceUnavailableException {
                Product prod = null;
            String url = getMethodURL() + "/" + String.valueOf(productId);
            prod = (Product) get(url, Product.class);
                return prod;
        }
        
        public List<Product> GetProductList() throws ServiceUnavailableException{
                List<Product> productList = new ArrayList<Product>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<Product>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                productList.add((Product)obj);
                        }
                } else 
                        return null;
                return productList;
        }
        
        public Category getCategory(int categoryId) throws ServiceUnavailableException{
                Category category = null;
            String url = getMethodURL() + "/" + String.valueOf(categoryId);
            category = (Category) get(url, Category.class);
                return category;
        }
        
        public  List<Category> GetCategoryTree() throws ServiceUnavailableException{
                List<Category> categoryList = new ArrayList<Category>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<Category>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                categoryList.add((Category)obj);
                        }
                } else 
                        return null;
                return categoryList;
        }
        
        public  List<Category> GetCategoryList() throws ServiceUnavailableException{
            List<Category> categoryList = new ArrayList<Category>();
            List<Object> objectList = null;
            Type listType = new TypeToken<ArrayList<Category>>(){}.getType();
            String url = getMethodURL();
            objectList = getList(url, listType);
            if (objectList != null){
                    for(Object obj : objectList){
                            categoryList.add((Category)obj);
                    }
            } else 
                    return null;
            return categoryList;
        }
        
        

        public Tax GetTax(int taxId) throws ServiceUnavailableException{
                Tax tax = null;
            String url = getMethodURL() + "/" + String.valueOf(taxId);
            tax = (Tax) get(url, Tax.class);
                return tax;
        }
        
        public List<Tax> GetTaxList() throws ServiceUnavailableException{
                List<Tax> taxList = new ArrayList<Tax>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<Tax>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                taxList.add((Tax)obj);
                        }
                } else 
                        return null;
                return taxList;
        }
        public Unit GetUnit(int unitId) throws ServiceUnavailableException{
                Unit unit = null;
            String url = getMethodURL() + "/" + String.valueOf(unitId);
            unit = (Unit) get(url, Unit.class);
                return unit;
        }
        
        public List<Unit> GetUnitList() throws ServiceUnavailableException{
                List<Unit> unitList = new ArrayList<Unit>();
                List<Object> objectList = null;
                Type listType = new TypeToken<ArrayList<Unit>>(){}.getType();
                String url = getMethodURL();
                objectList = getList(url, listType);
                if (objectList != null){
                        for(Object obj : objectList){
                                unitList.add((Unit)obj);
                        }
                } else 
                        return null;
                return unitList;
        }
}
